package de.clapp.backend;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

public class KeyCreator
{

    private static KeyPair keyPair;

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {

        KeyCreator.keyPair = KeyCreator.getKeyPair();
        System.out.println(KeyCreator.keyPair.getPublic());
        System.out.println(KeyCreator.keyPair.getPrivate());

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("publicKey"));
//        oos.writeObject(KeyCreator.keyPair.getPublic().getEncoded().toString());
        oos.writeObject(KeyCreator.keyPair.getPublic());

        oos.flush();
        oos.close();

        oos = new ObjectOutputStream(new FileOutputStream("privateKey"));
//        oos.writeObject(KeyCreator.keyPair.getPrivate().getEncoded().toString());
        oos.writeObject(KeyCreator.keyPair.getPrivate());

        oos.flush();
        oos.close();

        try (FileInputStream istream = new FileInputStream("publicKey"))
        {
            int read;
            int rowCount = 0;
            int arrayCount = 0;
            int[] buf = new int[16];
            while ((read = istream.read()) != -1)
            { // bis Datei-Ende lesen
                buf[arrayCount++] = read;
                if (arrayCount >= 15)
                {
                    // schreiben
                    System.out.format("%h", rowCount);
                    System.out.print(" ");
                    for (int i = 0; i < buf.length; i++)
                    {
                        System.out.format("%d", buf[i]);
                        System.out.print(" ");
                    }
                    System.out.println();
                    arrayCount %= 16;
                    rowCount++;
                }
            }
            istream.close();
        }
        catch (IOException e)
        {
            System.out.println(e.getMessage());
        }
    }

    /**
     * Uses a cipher to transform the bytes in an input stream and sends the
     * transformed bytes to an output stream.
     * 
     * @param in
     *            the input stream
     * @param out
     *            the output stream
     * @param cipher
     *            the cipher that transforms the bytes
     */
//    public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, GeneralSecurityException
//    {
//        int blockSize = cipher.getBlockSize();
//        int outputSize = cipher.getOutputSize(blockSize);
//        byte[] inBytes = new byte[blockSize];
//        byte[] outBytes = new byte[outputSize];
//
//        int inLength = 0;
//        ;
//        boolean more = true;
//        while (more)
//        {
//            inLength = in.read(inBytes);
//            if (inLength == blockSize)
//            {
//                int outLength = cipher.update(inBytes, 0, blockSize, outBytes);
//                out.write(outBytes, 0, outLength);
//            }
//            else
//                more = false;
//        }
//        if (inLength > 0)
//            outBytes = cipher.doFinal(inBytes, 0, inLength);
//        else
//            outBytes = cipher.doFinal();
//        out.write(outBytes);
//    }
//
//    private static final int KEYSIZE = 2048;

    /**
     * Erzeugt ein RSA Schluesselpaar
     * 
     * @return RSA Schluesselpaar
     * @throws Exception
     */
    public static KeyPair getKeyPair() throws Exception
    {
        if (keyPair == null)
        {
            KeyPairGenerator kpg;
            try
            {
                kpg = KeyPairGenerator.getInstance("RSA");
                kpg.initialize(2048);
                keyPair = kpg.generateKeyPair();
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new Exception("Fehler beim Erzeugen des Schluesselpaars: " + e.getMessage());
            }
        }

        return keyPair;
    }
}
